#!/usr/bin/perl
# 2014/01/28 Adjust for HOMER format
# 2010/07/19 Update version of Create_signalmap

use strict;
use warnings;
use IO::File;
use Getopt::Std;
use Carp qw(croak);
$| = 0;

if(@ARGV != 2 or $ARGV[0] eq '--help'){
	die "Usage : $0 -d [target directory]\n";
}


my %opt;
getopts("d:", \%opt);
my $directory = $opt{d};

(my $name = $directory) =~ s|/$||;	# remove last slash
if($name =~ m|.+/(.+)$|){
	$name = $1;
}

my $FILE_wig = sprintf "%s/%s.wig", $directory, $name;
my $FILE_tagInfo = sprintf "%s/%s", $directory, 'tagInfo.txt';

#---------------------------------------
# read tag information
#---------------------------------------
my $fragment_length = 0;
my $fh =  IO::File->new($FILE_tagInfo) or die "cannot open $FILE_tagInfo: $!";
while($_ = $fh->getline()){
	s/\r?\n//;
	my ($t1, $t2, $t3) = split /\t/;
	if($t1 =~ m/fragmentLengthEstimate=(\d+)/){
		$fragment_length = $1;
	}
}
$fh->close();
if($fragment_length == 0){
	die "cannot read average peak size inforamtion\n";
}

#---------------------------------------
# wig file file handle
#---------------------------------------
my $wig = IO::File->new($FILE_wig,'w') or die "cannot write $FILE_wig: $!";
my $headder = <<"EOF";
track type=wiggle_0 name=$name
EOF
$wig->print($headder);

#---------------------------------------
# alignment reads reading
#---------------------------------------
foreach my $chr(qw(I II III MTR MT)){
	my $file = sprintf "%s/%s.tags.tsv", $directory, $chr;
    my $fh = IO::File->new($file) or die "cannot open $file: $!";
	my %data;
    while($_ = $fh->getline()) {
		s/\r?\n//;

		### tsv format
		my ($none, $chr, $pos, $direction, $tags, $length) = split /\t/;

		if ($direction == 0) {
			my $start = $pos;
			my $end = $pos + ($fragment_length - 1);
			$data{$start} += $tags;
			$data{$end} -= $tags;
		}
		elsif ($direction == 1) {
			my $start = $pos - ($fragment_length + 1);
			my $end  = $pos;
			$start = 1 if ($start < 1);
			$data{$start} += $tags;
			$data{$end} -= $tags;
		}
		else {
			die "PROBLEM\n";
		}
    }
    $fh->close();

    ### calculate height
    my %ht;
    my @sorted_pos = sort {$a <=> $b} (keys %data);
    my $height = 0;
    foreach(my $pos = 1; $pos <= $sorted_pos[-1]; $pos++){
		if(exists $data{$pos}){
			$height += $data{$pos};
		}
		$ht{$pos} = $height;
	}


	### output
	$wig->printf("variableStep chrom=%s span=30\n", $chr);
	my $FILE_sgr = sprintf "%s/%s.sgr", $directory, $chr;
	my $sgr = IO::File->new($FILE_sgr, 'w') or die "cannot write $FILE_sgr: $!";

	my $SUM = 0;
	for(my $bp = 1; $bp <= 100; $bp++){
		$SUM += $ht{$bp};
	}

	$wig->printf("%d\t%f\n", 50, $SUM / 100);
	$sgr->printf("%d\t%f\n", 50, $SUM / 100);

	for(my $bp = 2; $bp <= $sorted_pos[-1] - 100; $bp++){
		my $removeInd = $bp - 1;
		$SUM -= $ht{$removeInd};

		my $addInd = $bp + 99;
		$SUM += $ht{$addInd};

		if($bp % 30 == 1){
			$wig->printf("%d\t%f\n", $bp+49, $SUM / 100);
		}
		$sgr->printf("%d\t%f\n", $bp+49, $SUM / 100);
	}
	$sgr->close();
}
$wig->close();






